<?php
namespace Addons\ElectricalMall\Controller;
use Think\ManageBaseController;

header('Content-type:text/html;charset=utf-8');
header('Access-Control-Allow-Origin:*');
class AdminController extends ManageBaseController {
	protected $need_login = false;
	protected $need_appinfo = false;

	public function beforeToDo() {
		# code...
		if (!session('?username') || !session('?shopid')) {
			# code...
			session('[destroy]');
			redirect(U('Admin/index'));
		}
	}

	public function login() {
		if (session('?username') && session('?shopid') && session('status_code') == 1) {
			# code...
			redirect(U('Admin/index'));
		}
		$this->display();
	}

	public function checkLogin() {
		# code...
		$username = I('username');
		$password = I('password');

		$condition['username'] = $username;
		$condition['password'] = md5($password);

		$res = M('electricalmall_user')->where($condition)->select();

		if ($res) {
			# code...
			session("username", $username);
			session("shopid", $res[0]['shop_id']);
			session("status_code", 1);
			$this->success('登录成功!', U('Admin/index'));
		} else {
			$this->error('账号或密码错误,请重试');
		}
	}

	public function logout() {
		# code...
		if (session('?username') || session('?status_code') || session('?shopid')) {
			# code...
			session('username', null);
			session('status_code', null);
			session('shopid', null);
			session('[destroy]');
		}
		redirect(U('Admin/login'));
	}

	/**
	 * [index description] 首页
	 * @return [type] [description]
	 */
	public function index() {
		# code...
		$this->beforeToDo();
		$this->display();
	}

	/**
	 * [classify description]  分类主页
	 * @return [type] [description]
	 */
	public function classify() {
		# code...
		$this->beforeToDo();
		$this->display();
	}

	/**
	 * [getClsData description]	获取分类树形数组
	 * @return [type] [description]
	 */
	public function getClsData() {
		# code...
		$this->beforeToDo();

		$res = M('electricalmall_classify')->where('shop_id=' . session('shopid'))->select();
		$data = $this->getClsTree($res, 0);
		// array_unshift($data, array('text' => '根目录', 'id' => 0, 'children' => []));
		$this->ajaxReturn(array('text' => '根目录', 'id' => 0, 'children' => $data));
	}

	/**
	 * [getClsTree description]	递归获取分类树形数组
	 * @param  [type] $data [description]
	 * @param  [type] $pid  [description]
	 * @return [type]       [description]
	 */
	private function getClsTree($data, $pid) {
		# code...
		foreach ($data as $key => $value) {
			# code...
			if ($value['parentid'] == $pid) {
				# code...
				$tmp['text'] = $value['cl_name'];
				$tmp['id'] = $value['id'];
				$tmp['children'] = $this->getClsTree($data, $value['id']);
				$tree[] = $tmp;
			}
		}
		return $tree;
	}

	/**
	 * [getClsDetail description]	根据id获取目标分类信息
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function getClsDetail($id) {
		# code...
		$res = M('electricalmall_classify')->where('id=' . $id)->select();
		$this->ajaxReturn($res);
	}

	/**
	 * [deleteClsData description]	删除分类及子分类
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function deleteClsData($id) {
		# code...
		$list = $this->getIdTree($id, $list);
		$res = M('electricalmall_classify')->delete(implode(',', $list)); //implode方法 数组变','符号隔开的字符串
		$this->ajaxReturn($res);
	}

	/**
	 * [getIdTree description]	获取该分类及其所有子分类的id数组
	 * @param  [type] $parentid [description]
	 * @param  [type] $list     [description]
	 * @return [type]           [description]
	 */
	private function getIdTree($parentid, $list) {
		# code...
		$list[] = $parentid;
		$res = M('electricalmall_classify')->where('parentid=' . $parentid)->select();
		foreach ($res as $key => $value) {
			# code...
			$list = $this->getIdTree($value['id'], $list);
		}
		return $list;
	}

	/**
	 * [formCls description]  分类表单页
	 * @param  [type] $parentid [description]
	 * @param  [type] $type     [description]
	 * @return [type]           [description]
	 */
	public function formCls($id, $type) {
		# code...
		$this->beforeToDo();

		switch ($type) {
		case 1:
			# code...
			$this->assign('name', '');
			$this->assign('img', "#");
			break;
		case 2:
			# code...
			$res = M('electricalmall_classify')->where('id=' . $id)->select();
			if ($res) {
				# code...
				$this->assign('name', $res[0]['cl_name']);
				$this->assign('img', $res[0]['cl_picture']);
			}
			break;
		default:
			# code...
			break;
		}

		$this->assign('id', $id);
		$this->assign('type', $type);
		$this->display();
	}

	public function updateCls() {
		# code...
		$this->beforeToDo();

		$this->ajaxReturn(I('photo'));
		// var_dump(I('.param'));
	}

	// public function formClsEdit($name, $img) {
	// 	# code...
	// 	$this->beforeToDo();
	// 	$this->assign('name', $name);
	// 	$this->assign('img', $img);
	// 	echo $img;
	// 	$this->display();
	// }

	/**
	 * [createCls description]创建分类
	 * @return [type] [description]
	 */
	public function createCls() {
		# code...
		$this->beforeToDo();

		$upload = new \Think\Upload(); // 实例化上传类
		$upload->maxSize = 0; // 设置附件上传大小,0为不限大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
		$upload->rootPath = './Addons/ElectricalMall/View/Public/images/upload/'; // 设置附件上传根目录
		$upload->savePath = ''; // 设置附件上传（子）目录

		$info = $upload->upload();

		if ($info) {
			$parentid = I('id');
			$cl_name = I('cl_name');

			$condition['parentid'] = $parentid;
			$condition['cl_name'] = $cl_name;
			$condition['shop_id'] = session('shopid');
			$condition['cl_picture'] = '/Addons/ElectricalMall/View/Public/images/upload/' . $info['photo']['savepath'] . $info['photo']['savename'];

			if ($parentid == 0) {
				# code...
				$rootid = $parentid;
			} else {
				$res = M('electricalmall_classify')->where('id=' . $parentid)->select();
				$rootid = $res[0]['rootid'] == 0 ? $res[0]['id'] : $res[0]['rootid'];
			}

			$condition['rootid'] = $rootid;
			$res = M('electricalmall_classify')->add($condition);
			if ($res) {
				# code...
				// $this->success('上传成功!');
				$data['msg'] = '上传成功';
				$data['status'] = '200';
				$data['data'] = '';
				$this->ajaxReturn($data);
				// return json_encode($data);
			} else {
				// $this->error('上传失败,请重试!');
				$data['msg'] = '上传失败,请重试';
				$data['status'] = '300';
				$data['data'] = '';
				$this->ajaxReturn($data);
				// return json_encode($data);
			}
		} else {
			// $this->error('上传失败,请重试!');
			$data['msg'] = '上传失败,请重试';
			$data['status'] = '300';
			$data['data'] = '';
			$this->ajaxReturn($data);
			// return json_encode($data);
		}
	}

}